/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.business.dao.CardPhotoMapper;
import com.rf.business.model.CardPhoto;
import com.rf.business.model.CardPhotoExample;
import com.rf.business.service.CardPhotoService;

@Service("cardPhotoService")
public class CardPhotoServiceImpl implements CardPhotoService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private CardPhotoMapper cardPhotoMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param cardPhotoId
	 * @return CardPhoto
	 */
   	@Override
	public CardPhoto getCardPhotoByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return cardPhotoMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据CardPhoto获取数据
	 * @param CardPhoto
	 * @return List<CardPhoto>
	 */
	@Override
	public List<CardPhoto> getCardPhoto(CardPhoto cardPhoto) throws Exception{
		// TODO Auto-generated method stub
		CardPhotoExample example = new CardPhotoExample();
		com.rf.business.model.CardPhotoExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<CardPhoto> list = cardPhotoMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<CardPhoto>
	 */
	@Override
	public List<CardPhoto> getCardPhoto() throws Exception{
		// TODO Auto-generated method stub
		CardPhotoExample example = new CardPhotoExample();
		com.rf.business.model.CardPhotoExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return cardPhotoMapper.selectByExample(example);
	}
	
	/**
	 * 根据车牌号获取数据
	 * @param carNo
	 * @return List<Map<String, Object>>
	 */
	@Override
	public List<Map<String, Object>> getCardPhotoByCarNo(String carNo) throws Exception{
		// TODO Auto-generated method stub
		String sql = "select cp.* from bus_card_release cr,bus_card_photo cp "
				+ " where cr.id = cp.card_id and cr.car_no='"+carNo+"' order by cr.create_time desc";
		List<Map<String, Object>> list = sqlAdapterMapper.selectSQL(sql);
		return list;
	}
   	
	/**
	 * 保存信息
	 * @param CardPhoto
	 * @return
	 */
	@Override
	public void addCardPhoto(CardPhoto cardPhoto) throws Exception{
		// TODO Auto-generated method stub
		cardPhotoMapper.insert(cardPhoto);
	}

	/**
	 * 修改数据
	 * @param CardPhoto
	 * @param
	 */
	@Override
	public void updateCardPhoto(CardPhoto cardPhoto) throws Exception{
		// TODO Auto-generated method stub
		cardPhotoMapper.updateByPrimaryKeySelective(cardPhoto);
	}
	
	/**
	 * 删除数据
	 * @param cardPhotoId
	 * @param
	 */
	@Override
	public void deleteCardPhoto(String id) throws Exception{
		// TODO Auto-generated method stub
		cardPhotoMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取数据分页
	 * @param CardPhoto
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getCardPhotoPage(CardPhoto cardPhoto,PageUtils pageUtils) throws Exception{
		CardPhotoExample example = new CardPhotoExample();
		com.rf.business.model.CardPhotoExample.Criteria criteria= example.createCriteria();
		
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<CardPhoto> list = cardPhotoMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

